﻿using System.Text.RegularExpressions;

namespace DelicateDict.Classes
{
    class DecorateMean
    {
        // Pattern of Rexg to filter mean parts 
        const string WordName = @"@(.*)+\n?";
        const string WordType = @"■(.*)+\n?";
        const string Mean     = @"●(.*)+\n?";
        const string WordExam = @"►(.+): ";
        const string MeanExam = @"(: </p>)((.+)+)\n?";
        const string Expression = @"⁞(.+)+\n";

        /// <summary>
        /// SetColor function.This function will set color of meaning
        /// </summary>
        /// <param name="mean"></param>
        public static void SetColor(ref string mean)
        {
            FilterMean(ref mean);
        }
        /// <summary>
        /// filter meaning into separate part and make them arounded by html element
        /// </summary>
        /// <param name="mean"></param>
        static void FilterMean(ref string mean)
        {
            string replacestring = ""; //this string hold somepart with a element html was added

            //Some character in orginal string of mean need to be replaced by special character.
            //This special charater is used to make a pattern for regular expression filter.
            mean = mean.Replace(@"\n*", @"\n■")
               .Replace(@"\n-", @"\n●")
               .Replace(@"\n=", @"\n►")
               .Replace("+", ": ")
               .Replace(@"\n!", @"\n⁞")
               .Replace(@"\n", "\n");
            // filter word name
            // 
            Regex reg = new Regex(WordName);
            foreach (Match m in reg.Matches(mean))
            {
                replacestring = "<h4>" + m.Value+ "</h4>";
                mean = mean.Replace(m.Value, replacestring);
            }
            // filter word type
            mean = Regex.Replace(mean,WordType,"<li class='wordtype'>$&</li>");
            // filter mean of word
            mean = Regex.Replace(mean, Mean, "<li class='mean'>$&</li>");
            // filter word of example
            mean = Regex.Replace(mean, WordExam, "<br/><p class='wordexam'>$&</p>");
            //filter mean of example
            reg = new Regex(MeanExam);
            foreach (Match m in reg.Matches(mean))
            {
                replacestring = "<p class='meanexam'>" + m.Groups[2].Value + "</p>";
                mean = mean.Replace(m.Groups[2].Value, replacestring);
            }
            // filter expression
            mean = Regex.Replace(mean, Expression, "<li class='expression'>$&</li>");
            // At the end, we remove all special character.
            mean = mean//.Replace(@"\n", "")
                             .Replace("■", "")
                             .Replace("●", "")
                             .Replace("►", "")
                             .Replace("⁞", "");
        }
    }
}
